/**
 * 
 */
package com.module.bp.condition;

/**
 * A {@link Condition} that requires that one or more 
 * {@link Condition}s pass in order to fire a {@link BaseEvent}.  
 * {@link LinkedCondition}s can be linked to other 
 * {@link LinkedCondition}s, creating a chain of 
 * {@link Condition}s that must be passed in order to 
 * fire a {@link BaseEvent}.
 * @author Donald
 *
 */
public final class LinkedCondition implements Condition {
	/** the primary {@link Condition}. */
	private Condition[] conditions;
	/**
	 * Creates a new instance of {@link LinkedCondition}.
	 * @param args the initial list of {@link Condition}s that must pass
	 */
	public LinkedCondition(final Condition... args) {
		if (args != null) {
			conditions = args;
		} else {
			conditions = new Condition[0];
		}
	}
	/**
	 * Adds a {@link Condition} that is required for passage.
	 * @param c the new {@link Condition}
	 */
	public void addCondition(final Condition c) {
		if (c != null) {
			Condition[] dest = new Condition[conditions.length + 1];
			System.arraycopy(conditions, 0, dest, 0, conditions.length);
			dest[conditions.length] = c;
			conditions = dest;
			dest = null;
		}
	}
	/**
	 * {@inheritDoc}
	 */
	public boolean passes() {
		boolean passes = true;
		// check all conditions
		for (int i = 0; i < conditions.length; i++) {
			passes = conditions[i].passes();
			// as soon as one condition fails, exit the loop
			if (!passes) {
				break;
			}
		}
		return passes;
	}
}
